<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="表单类型" prop="ftid">
        <!-- <el-input
          v-model="queryParams.ftid"
          placeholder="请输入表单类型ID"
          clearable
          @keyup.enter.native="handleQuery"
        /> -->
        <el-select v-model="queryParams.ftid" placeholder="请选择">
          <el-option v-for="item in FormtypeData" :key="item.ftid" :label="item.name" :value="item.ftid">
            {{ item.name }}{{ item.abbreviated }}
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="字段名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入字段名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="字段标识" prop="identify">
        <el-input v-model="queryParams.identify" placeholder="请输入字段标识" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['modularbusiness:formfield:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['modularbusiness:formfield:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['modularbusiness:formfield:remove']">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['modularbusiness:formfield:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="formfieldList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="字段ID" align="center" prop="ffid" />
      <el-table-column label="表单类型ID" align="center" prop="ftid" />
      <el-table-column label="字段名称" align="center" prop="name" />
      <el-table-column label="字段标识" align="center" prop="identify" />
      <el-table-column label="字段提示" align="center" prop="prompts" />
      <el-table-column label="是否禁用" align="center" prop="disables" />
      <el-table-column label="是否必填" align="center" prop="musts">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.musts == '0'">否</el-tag>
          <el-tag v-if="scope.row.musts == '1'" type="danger">是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="显示顺序" align="center" prop="sort" />
      <el-table-column label="审核顺序" align="center" prop="auditSort" />
      <el-table-column label="是否必填" align="center" prop="auditTf">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.auditTf == '0'">否</el-tag>
          <el-tag v-if="scope.row.auditTf == '1'" type="danger">是</el-tag>
        </template>
      </el-table-column>
      <!-- <el-table-column label="是否需要审核" align="center" prop="auditTf" /> -->
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['modularbusiness:formfield:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
            v-hasPermi="['modularbusiness:formfield:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改表单字段对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="700px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">



        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="表单类型" prop="ftid">
              <el-select v-model="form.ftid" placeholder="请选择">
                <el-option v-for="item in FormtypeData" :key="item.ftid" :label="item.name" :value="item.ftid">
                  {{ item.name }}{{ item.abbreviated }}
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入字段名称" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="显示顺序" prop="sort">
              <el-input-number controls-position="right" :min="0" v-model="form.sort" placeholder="请输入显示顺序" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段标识" prop="identify">
              <el-input v-model="form.identify" placeholder="请输入字段标识" />
            </el-form-item>
          </el-col>
        </el-row>


        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="需要审核" prop="auditTf">
              <el-select v-model="form.auditTf" placeholder="请选择">
                <el-option v-for="dict in dict.type.sys_0_1" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12" v-if="form.auditTf == 1">
            <el-form-item label="审核顺序" prop="auditSort">
              <el-input-number controls-position="right" :min="1" v-model="form.auditSort" placeholder="请输入审核顺序" />
            </el-form-item>
          </el-col>
        </el-row>

        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="禁用输入" prop="disables">
              <el-select v-model="form.disables" placeholder="请选择填表人是否能输入">
                <el-option v-for="dict in dict.type.sys_0_1" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="提示信息" prop="prompts">
              <el-input v-model="form.prompts" placeholder="请输入提示信息" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :span="24">
          <el-col :span="12">
            <el-form-item label="是否必填" prop="musts">
              <el-select v-model="form.musts" placeholder="填表人输入时是否必须填写">
                <el-option v-for="dict in dict.type.sys_0_1" :key="dict.value" :label="dict.label"
                  :value="dict.value"></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <!-- <el-form-item label="提示信息" prop="prompts">
              <el-input v-model="form.prompts" placeholder="请输入提示信息" />
            </el-form-item> -->
          </el-col>
        </el-row>
        <!-- <el-form-item label="是否禁用" prop="disables">
          <el-input v-model="form.disables" placeholder="请输入是否禁用" />
        </el-form-item> -->
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
        <!-- <el-form-item label="预留字段一" prop="reserved1">
          <el-input v-model="form.reserved1" placeholder="请输入预留字段一" />
        </el-form-item>
        <el-form-item label="预留字段二" prop="reserved2">
          <el-input v-model="form.reserved2" placeholder="请输入预留字段二" />
        </el-form-item>
        <el-form-item label="预留字段三" prop="reserved3">
          <el-input v-model="form.reserved3" placeholder="请输入预留字段三" />
        </el-form-item> -->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listFormfield, getFormfield, delFormfield, addFormfield, updateFormfield } from "@/api/formbox/formfield";
import { listFormtype } from "@/api/formbox/formtype";

export default {
  name: "Formfield",
  dicts: ['sys_0_1'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表单字段表格数据
      formfieldList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        ftid: null,
        name: null,
        identify: null,
        sort: null,
        prompts: null,
        auditSort: null,
        disables: null,
        musts: null,
        auditTf: null,
        reserved1: null,
        reserved2: null,
        reserved3: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      FormtypeData: [{
        ftid: 0,
        name: "正在查询",
        abbreviated: ""
      }],
      tfoptions: [{
        label: "是",
        value: 1,
      },
      {
        label: "否",
        value: 0,
      }]
    };
  },
  created() {
    this.getList();
    this.GetFormtype();
  },
  methods: {
    //查询表单类型
    GetFormtype() {
      let data = {
        pageNum: 1,
        pageSize: 100,
      };
      listFormtype(data).then(response => {
        this.FormtypeData = response.rows;
      });
    },
    /** 查询表单字段列表 */
    getList() {
      this.loading = true;
      listFormfield(this.queryParams).then(response => {
        this.formfieldList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        ffid: null,
        ftid: null,
        name: null,
        identify: null,
        sort: null,
        delFlag: null,
        auditSort: null,
        auditTf: null,
        prompts: null,
        createTime: null,
        updateTime: null,
        disables: null,
        musts: null,
        remark: null,
        reserved1: null,
        reserved2: null,
        reserved3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.ffid)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加表单字段";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const ffid = row.ffid || this.ids
      getFormfield(ffid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改表单字段";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.ffid != null) {
            updateFormfield(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addFormfield(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ffids = row.ffid || this.ids;
      this.$modal.confirm('是否确认删除表单字段编号为"' + ffids + '"的数据项？').then(function () {
        return delFormfield(ffids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('modularbusiness/formfield/export', {
        ...this.queryParams
      }, `formfield_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
